package com.ophiux.cs.api.consultBase.impl;

import java.lang.reflect.Type;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.ophiux.cs.api.consultBase.port.IDoctorApiPort;
import com.ophiux.cs.application.consultBase.dto.AddDoctorRequest;
import com.ophiux.cs.application.consultBase.dto.AddDoctorResponse;
import com.ophiux.cs.application.consultBase.dto.DeleteDoctorRequest;
import com.ophiux.cs.application.consultBase.dto.DeleteDoctorResponse;
import com.ophiux.cs.application.consultBase.dto.GetCSDoctorInfoByIDRequest;
import com.ophiux.cs.application.consultBase.dto.GetCSDoctorInfoByIDResponse;
import com.ophiux.cs.application.consultBase.dto.GetCSDoctorsRequest;
import com.ophiux.cs.application.consultBase.dto.GetCSDoctorsResponse;
import com.ophiux.cs.application.consultBase.dto.GetDoctorRequest;
import com.ophiux.cs.application.consultBase.dto.GetDoctorResponse;
import com.ophiux.cs.application.consultBase.dto.UpdateDoctorRequest;
import com.ophiux.cs.application.consultBase.dto.UpdateDoctorResponse;
import com.ophiux.cs.application.consultBase.service.IAddDoctorAppService;
import com.ophiux.cs.application.consultBase.service.IDeleteDoctorAppService;
import com.ophiux.cs.application.consultBase.service.IGetCSDoctorsAppService;
import com.ophiux.cs.application.consultBase.service.IGetDoctorAppService;
import com.ophiux.cs.application.consultBase.service.IUpdateDoctorAppService;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.Request;
import com.ophiux.cs.application.core.ResponsePage;
import com.ophiux.cs.application.core.Result;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class DoctorApiService implements IDoctorApiPort {
	@Autowired
    IAddDoctorAppService addDoctorAppService;
    @Autowired
    IGetDoctorAppService getDoctorAppService;
    @Autowired
    IUpdateDoctorAppService updateDoctorAppService;
    @Autowired
    IDeleteDoctorAppService deleteDoctorAppService;
    @Autowired
    IGetCSDoctorsAppService getCSDoctorsAppService;

    @Override
    public String getDoctor(String data) {
        log.info("getHospital接口,请求参数："+data);
        Request<GetDoctorRequest> request = null;
        Result<GetDoctorResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<GetDoctorRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("getHospital接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        GetDoctorRequest dto =request.getPARAM();
        result = getDoctorAppService.getDoctor(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("getDoctor接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
    public String GetCSDoctors(String data) {
        log.info("GetCSDoctors接口,请求参数："+data);
        Request<GetCSDoctorsRequest> request = null;
        Result<ResponsePage<GetCSDoctorsResponse>> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<GetCSDoctorsRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("GetCSDoctors接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        GetCSDoctorsRequest dto =request.getPARAM();
        result = getCSDoctorsAppService.getCSDoctors(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("getCSDoctors接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
	public String createDoctor(String data) {
		log.info("createDoctor接口,请求参数："+data);
        Request<AddDoctorRequest> request = null;
		Result<AddDoctorResponse> result = null;
        try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				@Override
				public Type getObtainType() {
					return new TypeToken<Request<AddDoctorRequest>>() {
					}.getType();
				}
			});
		} catch (Exception e) {
			log.info("createDoctor接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}

        AddDoctorRequest dto =request.getPARAM();
		result = addDoctorAppService.createDoctor(dto);

		String returnMessage = JsonUtils.toJson(result);
		log.info("createHospital接口,返回参数："+returnMessage);
		return returnMessage;
	}

    @Override
    public String updateDoctor(String data) {
        log.info("updateDoctor接口,请求参数："+data);
        Request<UpdateDoctorRequest> request = null;
        Result<UpdateDoctorResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<UpdateDoctorRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("updateHospital接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        UpdateDoctorRequest dto =request.getPARAM();
        result = updateDoctorAppService.updateDoctor(dto);
        String returnMessage = JsonUtils.toJson(result);
        log.info("updateHospital接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
    public String deleteDoctor(String data) {
        log.info("deleteDoctor接口,请求参数："+data);
        Request<DeleteDoctorRequest> request = null;
        Result<DeleteDoctorResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<DeleteDoctorRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("deleteHospital接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        DeleteDoctorRequest dto =request.getPARAM();
        result = deleteDoctorAppService.deleteDoctor(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("deleteDoctor接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
    public String GetCSDoctorInfoByID(String data) {
        log.info("【GetCSDoctorInfoByID】接口,请求参数："+data);
        Request<GetCSDoctorInfoByIDRequest> request = null;
        Result<GetCSDoctorInfoByIDResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<GetCSDoctorInfoByIDRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("【GetCSDoctorInfoByID】接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        GetCSDoctorInfoByIDRequest dto =request.getPARAM();
        result = getCSDoctorsAppService.GetCSDoctorInfoByID(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("【GetCSDoctorInfoByID】接口,返回参数："+returnMessage);
        return returnMessage;
    }
}
